package cn.sfa.common.taglib;

import java.io.File;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.jfree.util.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sfa.common.utils.CommonUtil;
import cn.sfa.entity.system.FunctionSysCode;
import cn.sfa.entity.system.ModelSysCode;
import cn.sfa.entity.users.SaleUsersLogin;

/**
 * 网页标题
 * @ClassName：MainTitleTag 
 * @Description：TODO
 * @author：LiXiang
 * @date：2012-6-8 下午02:39:47 
 *
 */
public class MainTitleTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1355396206595036725L;

	Logger logger = LoggerFactory.getLogger(MainTitleTag.class);

	@Override
	public int doEndTag() throws JspException {

		return super.doEndTag();
	}

	@Override
	public int doStartTag() throws JspException {

		String path = (String) pageContext.getRequest().getAttribute("path");

		String curURL = (String) pageContext.getRequest().getAttribute(
				"curPath");

		SaleUsersLogin user = (SaleUsersLogin) pageContext.getSession()
				.getAttribute("user");
		List userRole = user.getUserRoles(); // 用户角色
		List<FunctionSysCode> cacheFunction = CommonCache.getInstance()
				.getAllFunction(); // 功能缓存
		List<ModelSysCode> cacheMode = CommonCache.getInstance().getAllModel(); // 模块缓存
		String navURL = "";
		String menuURL = "";
		ModelSysCode currenMoudel = null; // 当前所在模块
		navURL = "/" + curURL.split("/")[1] + "/index"; // 导航匹配
		menuURL = curURL.substring(1, curURL.length()); // 左边菜单匹配
		logger.info("menuURL:" + menuURL + "  | navURL:" + navURL);
		if ("login".equals(curURL.split("/")[2])) {// 登录的时候进行菜单初始化
			navURL = "/news/index";
		}
		
		StringBuffer buffer = new StringBuffer();
		
		String title = "";
		try {
			for (int j = 0; j < cacheFunction.size(); j++) {
				if (cacheFunction.get(j).getFuncUrl().equals(menuURL)) {
					title = cacheFunction.get(j).getFuncName();
					
				}
			}
			if("".equals(title)){
				title = "CRM客户关系管理系统";
			}else{
				title += "-CRM客户关系管理系统";
			}
			
			buffer.append("<title>"+title+"</title>");
			JspWriter out = pageContext.getOut();
			out.println(buffer);// 输出到jsp页面
		} catch (Exception e) {
			Log.error(e.getMessage());
		}

		return super.doStartTag();
	}

}
